import { Vector as VectorSource } from 'ol/source';
import { Vector as VectorLayer } from 'ol/layer';
import { GeoJSON, WFS } from "ol/format";
import { getStyle } from './style'
import { layerConfig } from '@/config/config'
import {
    and as andFilter,
    like as likeFilter,
} from 'ol/format/filter';
import { intersects } from 'ol/format/filter';
import Feature from 'ol/Feature';
import { queryGeo } from '@/api/geoApi'
import { Message } from 'element-ui';

// 查询的方法
// type 查询类型
// layer 查询的图层
// params 查询的参数
export const search = async (type, layer, params) => {
    let features1, features2, features3 = []
    let source = new VectorSource()
    let filter = null
    switch (type) {
        case 'FuzzyQuery':
            filter = likeFilter('name', `*${params.name}*`)
            features1 = await queryMethod('xm_shoppoint', filter)
            features2 = await queryMethod('xm_lrdl', filter)
            break;
        case 'PropertyQuery':
            console.log('PropertyQuery')
            break;
        case 'GeoQuery':
            filter = intersects('geom', params.getGeometry())
            features1 = await queryMethod('xm_shoppoint', filter)
            features2 = await queryMethod('xm_lrdl', filter)
            features3 = await queryMethod('xm_building', filter)
            source.addFeatures([params])
            break;
    }
    source.addFeatures(features1)
    source.addFeatures(features2)
    source.addFeatures(features3)
    return source
}

// 添加查询的图层
export const searchLayer = new VectorLayer({
    name: 'search',
    source: new VectorSource(),
    style: (feature) => {
        let name = feature.get("name");
        return getStyle('search', false, null, name)
    },
    zIndex: 4
});

const queryMethod = async (layer, filter) => {
    let body = new WFS().writeGetFeature({
        srsName: "EPSG:4326", //坐标系
        featureNS: layerConfig.featureNS, // 注意这个值必须为创建工作区时的命名空间URI
        featurePrefix: layerConfig.featurePrefix, //工作区的命名
        featureTypes: [layer], //所要访问的图层
        outputFormat: "application/json",
        filter: filter, //属性过滤条件
    })
    let req = new XMLSerializer().serializeToString(body)

    let res = await queryGeo(req)
    if (res.status === 200) {
        let features = new GeoJSON({ geometryName: 'geom', }).readFeatures(res.data);
        return features
    } else {
        Message.error('查询图层失败');
        return
    }

    // await fetch(layerConfig.baseUrl, {
    //     method: "post",
    //     body: new XMLSerializer().serializeToString(
    //         new WFS().writeGetFeature({
    //             srsName: "EPSG:4326", //坐标系
    //             featureNS: layerConfig.featureNS, // 注意这个值必须为创建工作区时的命名空间URI
    //             featurePrefix: layerConfig.featurePrefix, //工作区的命名
    //             featureTypes: [layer], //所要访问的图层
    //             outputFormat: "application/json",
    //             filter: filter, //属性过滤条件
    //         })
    //     ),
    // }).then((response) => {
    //     return response.json();
    // }).then((json) => {
    //     // 格式要素
    //     features = new GeoJSON({ geometryName: 'geom', }).readFeatures(json);
    //     // features.forEach(v => {
    //     //     v.set('name', 'search')
    //     // })
    //     // let source = new VectorSource();
    //     // source.addFeatures(features)
    // });
}